/**
 * 
 */
package com.ai.dmc.utils.list;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ai.dmc.utils.Constants;

/**
 * Title： List<Map>对象排序
 * Description：  List<Map<String,Object>对象排序
 * Copyright：Copyright (c) 2009
 * Company：北京亚信智慧数据科技有限公司
 * @author lg, 2016年1月14日
 * @version 1.0
 */
public class ListSortUtil {
    /** 
     * @param list 要排序的集合 
     * @param key  要排序的Map的Key值 
     * @param sort desc 为降序  
     */  
    public static void sort( List<Map<String, String>> list, final String key, final String sort) {  
        // 用内部类实现排序  
        Collections.sort(list, new Comparator<Map<String, String>>() {  
  
            public int compare(Map<String, String> o1,Map<String, String> o2) {  
                int ret = 0;  
                try { 
                    if(Constants.SAME_FIELD_ORDER.equals(key)||Constants.LINK_FIELD_ORDER.equals(key)){
                        //o1，o2是list中的Map，可以在其内取得值，按其排序
                        Double  s1 = Double.parseDouble(o1.get(key));  
                        Double  s2 = Double.parseDouble(o2.get(key));   
                        if (sort != null && "desc".equals(sort)) {
                            //降序
                            ret = s2.compareTo(s1);
                        } else {  
                            // 正序排序  
                            ret = s1.compareTo(s2);
                        }  
                    }else{
                        //o1，o2是list中的Map，可以在其内取得值，按其排序
                        String  s1 = o1.get(key);  
                        String  s2 = o2.get(key);   
                          
                        if (sort != null && "desc".equals(sort)) {
                            //降序
                            ret = s2.compareTo(s1);  
                        } else {  
                            // 正序排序  
                            ret = s1.compareTo(s2);  
                        }  
                    }
                    
                } catch (IllegalArgumentException e) {  
                    e.printStackTrace(); 
                }
                return ret;  
            }
        });  
    }  
    
    public static void main(String[] args) {  
        ArrayList<Map<String,String>> list= new ArrayList<Map<String,String>>(); 
         Map<String,String> testMap1 = new HashMap<String,String>();
         Map<String,String> testMap2 = new HashMap<String,String>();
         Map<String,String> testMap3 = new HashMap<String,String>();
         Map<String,String> testMap4 = new HashMap<String,String>();
         Map<String,String> testMap5 = new HashMap<String,String>();
         testMap1.put("num","5");
         testMap1.put("name","zhangsan");
         testMap1.put("sex","男");
         
         testMap2.put("num","4");
         testMap2.put("name","lisi");
         testMap2.put("sex","男");
         
         testMap3.put("num","3");
         testMap3.put("name","wangwu");
         testMap3.put("sex","女");
         
         testMap4.put("num","无值");
         testMap4.put("name","zhaoliu");
         testMap4.put("sex","男");
         
         testMap5.put("num","1");
         testMap5.put("name","maqi");
         testMap5.put("sex","女");
         
        list.add(testMap1); 
        list.add(testMap3); 
        list.add(testMap2); 
        list.add(testMap5);
        list.add(testMap4); 
        for(Map<String,String> s:list){ 
            System.out.println("前"+s.get("num")+"&&"+s.get("name")+"&&"+s.get("sex"));  
        } 
        System.out.println("################################");  
        ListSortUtil.sort(list, "num", "asc");
        ArrayList<Map<String,String>> newList= new ArrayList<Map<String,String>>(); 
        for(Map<String,String> s:list){ 
            System.out.println("后"+s.get("num")+"&&"+s.get("name")+"&&"+s.get("sex"));
            newList.add(s);
        }  
        System.out.println("################################");
        for(Map<String,String> s:newList){ 
            System.out.println("##"+s.get("num")+"&&"+s.get("name")+"&&"+s.get("sex"));
        }  
    }
}
